function residual = dynamic_resid(T, y, x, params, steady_state, it_, T_flag)
% function residual = dynamic_resid(T, y, x, params, steady_state, it_, T_flag)
%
% File created by Dynare Preprocessor from .mod file
%
% Inputs:
%   T             [#temp variables by 1]     double   vector of temporary terms to be filled by function
%   y             [#dynamic variables by 1]  double   vector of endogenous variables in the order stored
%                                                     in M_.lead_lag_incidence; see the Manual
%   x             [nperiods by M_.exo_nbr]   double   matrix of exogenous variables (in declaration order)
%                                                     for all simulation periods
%   steady_state  [M_.endo_nbr by 1]         double   vector of steady state values
%   params        [M_.param_nbr by 1]        double   vector of parameter values in declaration order
%   it_           scalar                     double   time period for exogenous variables for which
%                                                     to evaluate the model
%   T_flag        boolean                    boolean  flag saying whether or not to calculate temporary terms
%
% Output:
%   residual
%

if T_flag
    T = model_htm.dynamic_resid_tt(T, y, x, params, steady_state, it_);
end
residual = zeros(36, 1);
lhs = params(1)*y(20);
rhs = params(4)*(1-params(3))*y(18)+y(24)*params(3)*T(1)*(-params(13))*(1-T(1))+(params(1)-params(4)*(1-params(3)))*y(29);
residual(1) = lhs - rhs;
lhs = (1-params(1))*y(21);
rhs = (1-params(3))*(1-params(4))*y(19)+y(24)*T(1)*params(3)*params(13)*(1-T(1))+y(29)*params(3)*(1-T(1));
residual(2) = lhs - rhs;
lhs = params(11)*y(22);
rhs = params(11)*params(9)*y(48)+(1-params(11))*(1-params(11)*params(9))*(y(29)*params(7)+params(8)*y(20)-y(24)*(1-T(1)));
residual(3) = lhs - rhs;
lhs = params(12)*y(23);
rhs = params(9)*params(12)*y(49)+(1-params(12))*(1-params(9)*params(12))*(y(29)*params(7)+params(8)*y(21)+T(1)*y(24));
residual(4) = lhs - rhs;
residual(5) = params(10)*y(36)-params(14)*y(28);
lhs = y(38);
rhs = (y(46)-y(20))*T(7);
residual(6) = lhs - rhs;
lhs = y(39);
rhs = (y(47)-y(21))*T(8);
residual(7) = lhs - rhs;
lhs = y(40);
rhs = T(4)*(y(52)-y(51));
residual(8) = lhs - rhs;
lhs = y(29)*params(7);
rhs = y(40)+params(7)*y(50)-T(5)*(y(28)-y(51))-y(38)-y(39);
residual(9) = lhs - rhs;
lhs = y(36);
rhs = T(1)*y(22)+y(23)*(1-T(1));
residual(10) = lhs - rhs;
lhs = y(27);
rhs = params(1)*y(22)+(1-params(1))*y(23);
residual(11) = lhs - rhs;
lhs = y(25);
rhs = params(1)*y(20)+(1-params(1))*y(21);
residual(12) = lhs - rhs;
lhs = y(26);
rhs = params(4)*y(18)+(1-params(4))*y(19);
residual(13) = lhs - rhs;
lhs = y(24);
rhs = y(22)+y(5)-y(23);
residual(14) = lhs - rhs;
lhs = y(11);
rhs = params(5)*y(1)+params(23)*x(it_, 1);
residual(15) = lhs - rhs;
lhs = y(12);
rhs = params(6)*y(2)+params(24)*x(it_, 2);
residual(16) = lhs - rhs;
lhs = y(18);
rhs = y(11)*0.16;
residual(17) = lhs - rhs;
lhs = y(19);
rhs = y(12)*0.16;
residual(18) = lhs - rhs;
lhs = y(42);
rhs = params(17)*y(9)+params(18)*(y(26)-y(6)+y(43));
residual(19) = lhs - rhs;
lhs = y(41);
rhs = y(9)*T(6)+(1-params(3))*(y(26)-y(6)+y(43)-y(42));
residual(20) = lhs - rhs;
lhs = y(43);
rhs = params(4)*y(44)+(1-params(4))*y(45);
residual(21) = lhs - rhs;
lhs = y(44);
rhs = y(36)+y(29)-y(7)+params(8)*(y(20)-y(3));
residual(22) = lhs - rhs;
lhs = y(45);
rhs = y(36)+y(29)-y(7)+params(8)*(y(21)-y(4));
residual(23) = lhs - rhs;
lhs = y(10);
rhs = y(11)+y(12);
residual(24) = lhs - rhs;
lhs = y(23);
rhs = y(31)-y(8);
residual(25) = lhs - rhs;
lhs = y(30);
rhs = y(24)+y(31)/T(1);
residual(26) = lhs - rhs;
lhs = y(32);
rhs = y(29)+y(26);
residual(27) = lhs - rhs;
lhs = y(33);
rhs = y(28)-(T(1)*y(48)+y(49)*(1-T(1)));
residual(28) = lhs - rhs;
lhs = y(34);
rhs = params(3)*y(29);
residual(29) = lhs - rhs;
lhs = y(35);
rhs = (1-params(3))*y(26);
residual(30) = lhs - rhs;
lhs = y(14);
rhs = y(35)*(1-params(3))*params(4)/params(1);
residual(31) = lhs - rhs;
lhs = y(15);
rhs = y(35)*(1-params(3))*(1-params(4))/(1-params(1));
residual(32) = lhs - rhs;
lhs = y(37);
rhs = y(28)*12;
residual(33) = lhs - rhs;
lhs = y(16);
rhs = (-(y(29)*params(7)+params(8)*y(20)-y(24)*(1-T(1))));
residual(34) = lhs - rhs;
lhs = y(17);
rhs = (-(y(29)*params(7)+params(8)*y(21)+T(1)*y(24)));
residual(35) = lhs - rhs;
lhs = y(13);
rhs = params(1)*y(16)+(1-params(1))*y(17);
residual(36) = lhs - rhs;

end
